package com.snailvr.tv.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.util.Util;
import com.makeramen.roundedimageview.RoundedImageView;
import com.snail.readyvr.R;
import com.snail.readyvr.util.EventBus;
import com.snailvr.tv.event.ChannelChangeEvent;
import com.snailvr.tv.network.entity.ChannelListResponBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/19.
 */
public class ChannelAdapter extends RecyclerView.Adapter<ChannelAdapter.ViewHolder>{
    private Context mContext;
    private List<ChannelListResponBean.DataBean.ListBean> mChnlistBeen;
    private int mChannel_id;


    public ChannelAdapter(Context _context, List<ChannelListResponBean.DataBean.ListBean> chnlistBeen, int channel_id) {
        super();
        mContext = _context;
        mChnlistBeen = chnlistBeen;
        mChannel_id = channel_id;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View _view = LayoutInflater.from(mContext).inflate(R.layout.layout_channel_item, parent, false);
        final ViewHolder mHolder = new ViewHolder(_view);
        _view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new ChannelChangeEvent(mChnlistBeen.get(mHolder.getAdapterPosition())));
                ((Activity)mContext).finish();

            }
        });
        return mHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ChannelListResponBean.DataBean.ListBean _Bean = mChnlistBeen.get(position);
        if (Util.isOnMainThread()){
            Glide.with(mContext)
                    .load(_Bean.getThumb())
                    .diskCacheStrategy(DiskCacheStrategy.RESULT)
                    .into(holder.mImageViewBg);
        }
        holder.mTextViewName.setText(_Bean.getName());
        if (_Bean.getId() == mChannel_id) {
            holder.mLayout.setBackgroundResource(R.drawable.shape_round_layout_5_blue);
            holder.mImageViewLive.setVisibility(View.VISIBLE);
            holder.mImageViewPlay.setVisibility(View.GONE);
        } else {
            holder.mLayout.setBackgroundResource(0);
            holder.mImageViewLive.setVisibility(View.GONE);
            holder.mImageViewPlay.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return mChnlistBeen.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder
    {
        @BindView(R.id.layout_root)
        LinearLayout mLayout;
        @BindView(R.id.img_view_channel_bg)
        RoundedImageView mImageViewBg;
        @BindView(R.id.txt_view_channel_name)
        TextView mTextViewName;
        @BindView(R.id.img_view_live)
        ImageView mImageViewLive;
        @BindView(R.id.img_view_play_button)
        ImageView mImageViewPlay;
        public ViewHolder(View _view) {
            super(_view);
            ButterKnife.bind(this, _view);
        }
    }
}
